
package com.jf.cloud.service.sysmanager.remote.api;

import com.jf.cloud.service.sysmanager.common.base.OPaging;
import com.jf.cloud.service.sysmanager.common.db.Query;
import com.jf.cloud.service.sysmanager.common.taglib.Paging;
import com.jf.cloud.service.sysmanager.common.util.StringUtil;
import com.jf.cloud.service.sysmanager.dao.IPubgBaseAfficheService;
import com.jf.cloud.service.sysmanager.entity.PubgBaseAffiche;
import com.jf.cloud.service.sysmanager.handler.TokenHandler;
import com.jf.cloud.service.sysmanager.support.entity.Result;
import com.jf.cloud.service.sysmanager.support.web.BaseController;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公告控制器
 * 
 * @author 代码机1.0 创建时间：2018-03-31
 */
@Controller
@RequestMapping("/appPubgBaseAffiche")
public class AppPubgBaseAfficheController extends BaseController {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(AppPubgBaseAfficheController.class);

	@Resource
	private TokenHandler tokenHandler;
	
	@Resource
	private IPubgBaseAfficheService pubgBaseAfficheService;

	/**
	 * 公告列表
	 *
	 * @author 高冬冬 创建时间：2018-04-08
	 */
	@ResponseBody
	@RequestMapping("/baseAfficheList")
	public Map<String, Object> baseAfficheList(OPaging<PubgBaseAffiche> page, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", -1);
		result.put("message", "获取公告列表失败！");
		try {
			String token = getParamValueString(request, "token");
			String userId = tokenHandler.getCommUserInfoByToken(token);
			if (userId == null || userId.trim().length() == 0) {
				result.put("code", -2);
				result.put("message", "请重新登陆！");
				return result;
			}
			// 每页数据条数
			int limit = getParamValueInteger(request, "limit", 50);
			page.setLimit(limit);

			// 第几页
			int offset = getParamValueInteger(request, "offset", -1);
			if (offset > 0) {
				page.setOffset((offset-1)*limit);
			}

            //返回的数据集合
			Map<String,String> columns = new HashMap<String,String>();
			columns.put("ROW_ID", "rowId");
			columns.put("NOTICE_TITLE", "noticeTitle");
			columns.put("IMG_PATH", "imgPath");
			columns.put("IFNULL(OPT_TIME,CREATE_TIME)", "optTime");

			page.getWm().put("FB_FLAG", 1);//发布
			page.getWm().put("DEL_FLAG", 0);//未删除
			page.setOrderBy(" ORDER BY ORDER_BY_NO, OPT_TIME DESC, CREATE_TIME DESC ");
            Paging<PubgBaseAffiche> pageTemp = pubgBaseAfficheService.findPage(columns, page);
			result.put("data", pageTemp.getDataList());
			result.put("code", 0);
			result.put("message", "获取公告列表成功！");
		} catch (Exception e) {
			log.error("获取公告列表出错！", e);
		}
		return result;
	}

    /**
     * 获取公告详情
     *
     * @author 高冬冬 创建时间：2018-04-08
     */
    @ResponseBody
    @RequestMapping("/baseAfficheDetail")
    public Result<PubgBaseAffiche> baseAfficheDetail(String token,Long rowId, HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Result<PubgBaseAffiche> result = new Result<PubgBaseAffiche>();
        result.setCode(-1);
        result.setMessage("获取公告详情失败！");
        try {
            String userId = tokenHandler.getCommUserInfoByToken(token);
            if (userId == null || userId.trim().length() == 0) {
                result.setCode(-2);
                result.setMessage("请重新登陆！");
                return result;
            }
            if (StringUtil.isNull(rowId + "")) {
                result.setMessage("id不合法！");
                return result;
            }
			Map<String,String> columns = new HashMap<String,String>();
			columns.put("ROW_ID", "rowId");
			columns.put("NOTICE_TITLE", "noticeTitle");
			columns.put("IMG_PATH", "imgPath");
			columns.put("NOTICE_CONTENT", "noticeContent");
			columns.put("REMARK", "remark");
			columns.put("IFNULL(OPT_TIME,CREATE_TIME)", "optTime");
            Query query = new Query();
            query.put("ROW_ID", rowId);
            query.put("FB_FLAG", 1);//发布
            query.put("DEL_FLAG", 0);//未删除
            PubgBaseAffiche pubgBaseAffiche = this.pubgBaseAfficheService.find(columns,query);
            result.setData(pubgBaseAffiche);
            result.setCode(0);
            result.setMessage("获取公告详情成功！");
        } catch (Exception e) {
            log.error("获取公告详情失败！", e);
        }
        return result;
    }
}
